class CBulletManager {
    constructor() {
        this._allBullet = {};
        this._bulletid = 1;
    }

    update() {
        this.moveToMonster();
    }

    clear() {
        for (let id in this._allBullet) { 
            this.removeBullet(this._allBullet[id]);
        }
    }

    //添加子弹
    addBullet(bullet) {
        bullet.id = this._bulletid++;
        this._allBullet[bullet.id] = bullet;
    }

    removeBullet(bullet) {
        bullet.destroy(); //销毁该节点，在本帧末尾回收到对象池

        if (this._allBullet[bullet.id]) {
            delete this._allBullet[bullet.id]; //delete  删除对象中某个键
        }
    }

    moveToMonster() {
        let speed = 4;
        for (let id in this._allBullet) {
            let bullet = this._allBullet[id];
            bullet.x += speed * Math.cos(bullet.hd);
            bullet.y += speed * Math.sin(bullet.hd);
            let dis = bullet.getPosition().sub(bullet.startpos); //计算子弹与起始点的距离
            if (dis.mag() > 1000) {
                this.removeBullet(bullet);
            }
        }
    }
}

window.g_Bullet = new CBulletManager();
